package com.cby.jetpack.common.widget.snackbar

import android.view.View
import android.widget.TextView
import com.cby.jetpack.common.R
import com.google.android.material.snackbar.Snackbar

/**
 * 给SnackBar设置颜色的
 */
object ColoredSnackbar {
    private const val red = -0x46afb0
    private const val green = -0xb350b0
    private const val blue = -0xde6a0d
    private const val orange = -0x3ef9
    private const val black = -0xd1d1d2
    private const val white = -0x1
    private const val nightTextColor = -0x655314
    private const val nightBgColor = -0xd5d0bf
    private fun getSnackBarLayout(snackbar: Snackbar?): View? {
        return snackbar?.view
    }

    private fun colorSnackBar(snackbar: Snackbar, bgColorId: Int, textColorId: Int): Snackbar {
        val snackBarView = getSnackBarLayout(snackbar)
        if (snackBarView != null) {
            //设置透明度
            snackBarView.alpha = 0.95f
            //设置背景色
            snackBarView.setBackgroundColor(bgColorId)
            //这只内容文字的颜色
            (snackBarView.findViewById<View>(R.id.snackbar_text) as TextView).setTextColor(
                textColorId
            )
        }
        return snackbar
    }

    @JvmStatic
    fun defaultInfo(snackbar: Snackbar): Snackbar {
        return colorSnackBar(snackbar, black, white)
    }

    fun defaultInfoNight(snackbar: Snackbar): Snackbar {
        return colorSnackBar(snackbar, nightBgColor, nightTextColor)
    }

    @JvmStatic
    fun info(snackbar: Snackbar): Snackbar {
        return colorSnackBar(snackbar, blue, white)
    }

    @JvmStatic
    fun warning(snackbar: Snackbar): Snackbar {
        return colorSnackBar(snackbar, orange, white)
    }

    @JvmStatic
    fun alert(snackbar: Snackbar): Snackbar {
        return colorSnackBar(snackbar, red, white)
    }

    @JvmStatic
    fun confirm(snackbar: Snackbar): Snackbar {
        return colorSnackBar(snackbar, green, white)
    }
}